#include "StatusViewer.h"
#include "../Controllers/DotController.h"
#include "../Settings/GameSettings.h"
#include "../Models/DotModel.h"

#define NEWGAME "New Game"
#define CANCELGAME "Cancel Game"

StatusViewer* StatusViewer::mpMe = NULL;

StatusViewer::StatusViewer(QStatusBar *pSB, DotController *pController,QObject *pParent):QObject(pParent),
    mpStatusBar(pSB),mpController(pController)
{
    showTempMsg(tr("Application Started"),LONG_DURATION);
    mpStartCancelGame = new QPushButton(NEWGAME);
    pSB->addPermanentWidget(mpStartCancelGame);

    connect(mpStartCancelGame,SIGNAL(clicked()),SLOT(gameStartStop()));
    connect(pSB,SIGNAL(messageChanged(QString)),SLOT(msgChanged(QString)));
    connect(mpController,SIGNAL(gameStarted()),SLOT(gameStarted()));
    connect(mpController,SIGNAL(lineAdded(Game::Player)),SLOT(lineAdded(Game::Player)));
    connect(mpController,SIGNAL(cellOwned(Game::Player)),SLOT(cellOwned(Game::Player)));
    connect(mpController,SIGNAL(gameTurnChanged(Game::Player)),SLOT(gameTurnChanged(Game::Player)));
    connect(mpController,SIGNAL(gameFinished(Game::Player)),SLOT(gameFinished(Game::Player)));
    connect(mpController,SIGNAL(gameCanceled()),SLOT(gameCancelled()));
}

StatusViewer* StatusViewer::me(QStatusBar *pSB, DotController *pController)
{
    if(mpMe == NULL){
        if(pSB != NULL &&  pController != NULL){
            mpMe = new StatusViewer(pSB,pController);
        }
    }

    if(mpMe == NULL){
        qDebug("Staus Manager is Null");
    }

    return mpMe;
}

void StatusViewer::msgChanged(const QString &msg)
{
    if(msg.isNull()){
        showTempMsg(QString("Press Ctrl+I to view instructions."));
    }
}

void StatusViewer::showTempMsg(QString msg, int timeout)
{
    mpStatusBar->showMessage(msg,timeout);
}

void StatusViewer::gameStarted()
{
    showTempMsg(QString("Game Started"),LONG_DURATION);
    setStartCancelButtonText(CANCELGAME);
}

void StatusViewer::gameTurnChanged(Game::Player player)
{   
    //showTempMsg("Now " + GameSettings::me()->playerName(player) + "'s turn...",LONG_DURATION);
}

void StatusViewer::lineAdded(Game::Player owner)
{
    showTempMsg(QString("line added by ") + GameSettings::me()->playerName(owner),LONG_DURATION);
}

void StatusViewer::cellOwned(Game::Player owner)
{
    showTempMsg(QString("cell owned by ") + GameSettings::me()->playerName(owner),LONG_DURATION);
}

void StatusViewer::gameFinished(Game::Player winner)
{
    QString strWinInfo = winner != Game::Unknown ?
                         (QString("Game finished and the WINNER is ") +
                          GameSettings::me()->playerName(winner) + QString(" (") +
                          QString::number(mpController->model()->playerCellsCount(winner)) + QString("/") +
                          QString::number(mpController->model()->totalCells()) +QString(")") ) : QString("Game Drawn!!!");
    showTempMsg(strWinInfo);
    setStartCancelButtonText(NEWGAME);
}

void StatusViewer::gameCancelled()
{
    showTempMsg(QString("Game cancelled"),LONG_DURATION); 
    setStartCancelButtonText(NEWGAME);
}

void StatusViewer::gameStartStop()
{

    if(mpStartCancelGame->text() == NEWGAME){
        if(mpController->startGame()){
            setStartCancelButtonText(CANCELGAME);
        }
    }
    else if(mpStartCancelGame->text() == CANCELGAME){
        if(mpController->cancelGame(true,true)){
            setStartCancelButtonText(NEWGAME);
        }
    }

}

void StatusViewer::setStartCancelButtonText(QString txt)
{
    mpStartCancelGame->setText(txt);
}
